
#include "xdns-server-selector-impl.h"

using namespace std;

XdnsServerSelector::XdnsServerSelector()
{
}

XdnsServerSelector::~XdnsServerSelector()
{
}

XdnsError
XdnsServerSelector::Init(const vector<XdnsServerState*>& serverStates,
                         XdnsServerSelector** serverSelector)
{
    if ((serverStates.empty()) ||
        (NULL == serverSelector))
    {
        return XE_Inval;
    }

    // Check if any pointer to the server state is NULL.
    vector<XdnsServerState*>::const_iterator iter = serverStates.begin();
    vector<XdnsServerState*>::const_iterator end = serverStates.end();
    for (; iter != end; ++iter)
    {
        if (NULL == (*iter))
        {
            return XE_Inval;
        }
    }

    /* Create a new server selector and it does not take the
       ownership of the server states.
    */
    XdnsServerSelectorImpl* serverSelectorImpl = new (nothrow) XdnsServerSelectorImpl(serverStates);
    if (NULL == serverSelectorImpl)
    {
        return XE_NoMem;
    }

    *serverSelector = serverSelectorImpl;
    return XE_Ok;
}

void
XdnsServerSelector::Release(XdnsServerSelector* serverSelector)
{
    if (serverSelector != NULL)
    {
        delete serverSelector;
    }
}

